# -*- coding: utf-8 -*-
import Image
from django.contrib.admin import forms
from django.db import models
from datetime import datetime
from django.contrib.auth.models import User

COMBUSTIVEL_CHOICES = (('1','Alcool'),('2','Gasolina'),('3','Diesel'),('4','Flex'))
PORTAS_CHOICES = (('1','4 portas'),('2','2 portas'))

from botepreco.thumbs import ImageWithThumbsField
def content_image_name(instance, filename):
    return '/'.join(['imagens/carro', filename])
def imagem_galeria(instance, filename):
    return '/'.join(['imagens/carro', filename])

class Colaborador(models.Model):
    user = models.ForeignKey(User, unique=True)
    nome = models.CharField(max_length=100)
    email = models.EmailField()
    telefone = models.CharField(max_length=12)
    twitter = models.URLField(max_length=50, blank=True, null=True, verify_exists=False)
    facebook = models.URLField(max_length=50, blank=True, null=True, verify_exists=False)
    googleplus = models.URLField(max_length=50, blank=True, null=True, default="https://plus.google.com/117251973452118308738", verify_exists=False)
    dataentrada = models.DateField(auto_now_add=True)
    foto = models.ImageField(upload_to='colaborador', blank=True, null=True)

    class Meta:
        verbose_name_plural = "Colaboradores"
    def __unicode__(self):
        return self.nome

class Vendedor(models.Model):
    nome = models.CharField(max_length=100)
    email = models.EmailField()
    contato1 = models.CharField('Contato',max_length=20, help_text='XX XXXX-XXXX')
    contato2 = models.CharField('Contato',max_length=20, help_text='XX XXXX-XXXX')
    telefone = models.CharField(max_length=12)
    facebook = models.URLField(max_length=50, blank=True, null=True, verify_exists=False)
    dataentrada = models.DateField(auto_now_add=True)
    foto = models.ImageField(upload_to='colaborador', blank=True, null=True)

    class Meta:
        verbose_name_plural = "Vendedores"
    def __unicode__(self):
        return self.nome

class Marca(models.Model):
    nome = models.CharField(max_length=100)
    dataCadastro = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name_plural = "Cadastrar Marca"
    def __unicode__(self):
        return self.nome

class Modelo(models.Model):
    nome = models.CharField(max_length=100)
    marca = models.ForeignKey(Marca)
    dataCadastro = models.DateTimeField(default=datetime.now)
    usuario = models.CharField(max_length=100)

    def Usuario(self):
        self.usuario = User.first_name

    class Meta:
        verbose_name_plural = "Cadastrar Modelo"
    def __unicode__(self):
        return self.nome

class Cor(models.Model):
    nome = models.CharField(max_length=100)
    dataCadastro = models.DateTimeField(default=datetime.now)

    class Meta:
        verbose_name_plural = "Cadastrar de Cores"
    def __unicode__(self):
        return self.nome

class Carros(models.Model):
    publicar = models.BooleanField(default=True)
    principal = models.BooleanField()
    topo = models.BooleanField()
    vendido = models.BooleanField()
    valor = models.CharField('Valor vendido', max_length=100,help_text='13.000', blank=True, null=True)
    vendedor = models.ForeignKey(Vendedor, blank=True, null=True)
    observacaoVenda = models.TextField('Observação Venda', blank=True, null=True)
    placa = models.CharField(max_length=100, unique=True)
    marca = models.ForeignKey(Marca)
    modelo = models.ForeignKey(Modelo)
    cor = models.ForeignKey(Cor, blank=True, null=True)
    imagemDestaque = ImageWithThumbsField('Imagem Destaque', upload_to=content_image_name, sizes=((200,200),(300,225)))
    fotos = models.ImageField(upload_to='imagens/carro', blank=True, null=True)
    custo = models.CharField(max_length=100,help_text='2.200')
    preco = models.CharField(max_length=100,help_text='3.400')
    combustivel = models.CharField(max_length=100, choices=COMBUSTIVEL_CHOICES)
    ano = models.CharField(max_length=100,blank=True, null=True)
    zero = models.BooleanField()
    kilometragem = models.CharField(max_length=100,help_text='34.800', blank=True, null=True)
    portas = models.CharField(max_length=100, choices=PORTAS_CHOICES)
    motor = models.CharField(max_length=100)
    versao = models.CharField(max_length=100, blank=True)
    captacao = models.CharField(max_length=100, blank=True, null=True)
    nomeProprietario = models.CharField('Proprietário',max_length=300)
    contato1 = models.CharField('Telefone', max_length=100, help_text='(0**86)3212-3172')
    contato2 = models.CharField('Telefone',max_length=100, help_text='(0**86)3212-3172', blank=True, null=True)
    contato3 = models.CharField('Telefone',max_length=100, help_text='(0**86)3212-3172', blank=True, null=True)
    email = models.EmailField()
    basico = models.BooleanField('Blasico',blank=True)
    ar = models.BooleanField('Ar Consicionado',blank=True)
    hidraulica = models.BooleanField('Direção Hidraulica', blank=True)
    vidro = models.BooleanField('Vidro Elétrico',blank=True)
    trava = models.BooleanField('Trava Elétrico',blank=True)
    alarme = models.BooleanField('Alarme',blank=True)
    som = models.BooleanField('Som',blank=True)
    DesembacadorTraseiro = models.BooleanField('Desembaçador Traseiro',blank=True)
    reboque = models.BooleanField('Reboque',blank=True)
    retroEle = models.BooleanField('Retrovisor Elétrico',blank=True)
    air = models.BooleanField('AIR BAG',blank=True)
    frioAbs = models.BooleanField('Freio ABS',blank=True)
    gps = models.BooleanField('GPS',blank=True)
    neblina = models.BooleanField('Farol de Neblina',blank=True)
    sensor = models.BooleanField('Sensor de Estacionamento',blank=True)
    ligaLeve = models.BooleanField('Rodas de Liga Leve',blank=True)
    couro = models.BooleanField('Banco de Couro',blank=True)
    limpadorTraseiro = models.BooleanField('limpador Traseiro',blank=True)
    volanteAltura = models.BooleanField('Altura do Volante',blank=True)
    capotaMar = models.BooleanField('Capota Marítima',blank=True)
    protetorCacamba = models.BooleanField('Protetor Caçamba',blank=True)
    controleTracao= models.BooleanField('Controle de Tração',blank=True)
    quatroPdois = models.BooleanField('4X2',blank=True)
    quatroPquatro = models.BooleanField('4X4',blank=True)
    colunaDirecao = models.BooleanField('Ajuste Coluna de Direção',blank=True)
    cambioManual = models.BooleanField('Câmbio Manual',blank=True)
    cambioAutomatico = models.BooleanField('Câmbio Automático', blank=True)
    compBordo = models.BooleanField('Computador de Bordo',blank=True)
    pilotoAutomatico = models.BooleanField('Piloto Automático',blank=True)
    tetoSolar = models.BooleanField('Teto Solar', blank=True)
    observacao = models.TextField('Observação', blank=True, null=True)
    dataEntrada = models.DateTimeField('Data Cadastro',default=datetime.now)
    cadastro = models.ForeignKey(Colaborador)
    thumbnail = models.ImageField(null=True,blank=True,upload_to='galeria/thumbnail',)
    slugCarro = models.SlugField(blank=True)


    def __unicode__(self):
        return self.modelo.__str__()

    def placaMaiuscula(self):
        placa = self.placa.isupper()
        return placa

    class Meta:
        verbose_name_plural = "Cadastrar Carros"

class Imagem(models.Model):
    foto = ImageWithThumbsField('Fotos dos carros', upload_to=imagem_galeria, sizes=((200,200),(300,225)))
    nomeGaleria = models.ForeignKey(Carros)

#SIGNALS
from django.db.models import signals
from django.template.defaultfilters import slugify

def carros_pre_save(signal, instance, sender, **kwargs):
    instance.slug = slugify(instance.modelo)

signals.pre_save.connect(carros_pre_save, sender=Carros)
